﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.DBModule;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.BLL;
using ZX.Mapping;
using ZX.Web.UI.Controls.ControlBind;
using ZX.WebApp.CrmWorkFlowService;
using System.Data;

namespace ZX.WebApp.Crm.Modules
{
    public partial class CrmValueTask : EditUIPage
    {
        private string strId = "", wfid = "", strDeliverTrainInfo = "[]", wtid = "";
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmDeliverMajor bllDeliverMajorProject = new BCrmDeliverMajor();
        ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient client = new ZX.WebApp.CrmWorkFlowService.WorkFlowServiceClient();
        public string workflowId
        {
            get;
            set;
        }
        public string workflowInsId
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["WorkflowInsId"] != null)
            {
                workflowInsId = Request["WorkflowInsId"].ToString();
                Inits(workflowInsId);
            }
            if (!IsPostBack)
            {
                bindProjectType();
                BaseEntity instance = ZX.Common.Globals.getEntity("CrmValue") as BaseEntity;
                BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
                BaseEntity entity = bInstance.GetEntityById("") as BaseEntity;
                this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);
                this.ID.Value = "";
                //客户类型
                IList<DatPhraseDetail> ListDetailType = bllDetail.GetDatPhraseDetailByClassValue("CustomerType");
                DropDownListControl.DropDataSourceBind(this.ddlTrans, ListDetailType, "PhraseText", "PhraseID");
                //交付单状态
                IList<DatPhraseDetail> ListDliverId = bllDetail.GetDatPhraseDetailByClassValue("DeliverState");
                DropDownListControl.DropDataSourceBind(this.DliverState, ListDliverId, "PhraseText", "PhraseID");
                //产品
                IList<CrmProduct> crmProduct = bllDetail.GetCrmProduct();
                DropDownListControl.DropDataSourceBind(this.ddlAgainProductName, crmProduct, "Caption", "Productid");
                for (int i = 2012; i <= DateTime.Now.Year; i++)
                {
                    ListItem li = new ListItem();
                    li.Text = i.ToString();
                    li.Value = i.ToString();
                    ddlYear.Items.Add(li);
                }
                
                if (Flag == "0")
                {
                    this.DeliverDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    this.ID.Value = Guid.NewGuid().ToString();
                    DliverState.SelectedIndex = 0;
                    this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                    this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
                }
                if (Flag == "1")
                {
                    strId = this.GetStringFromQueryString("id", "");
                    if (strId == "")
                    {
                        strId = this.GetStringFromQueryString("WorkflowInsId", "");
                    }
                    IList<V_CrmValue> instanceMajor = bllDeliverMajorProject.GetAllCrmValue(new Guid(strId));
                    if (instanceMajor != null && instanceMajor.Count > 0)
                    {
                        wfid = instanceMajor[0].WorkFlowId;
                        wtid = instanceMajor[0].WorkTaskId;
                        if (strDescript == "nxja")
                        {
                            DliverState.SelectedIndex = 4;
                        }
                        else if (strDescript == "nxqd")
                        {
                            DliverState.SelectedIndex = 2;
                        }
                        else
                        {
                            DliverState.SelectedIndex = 2;
                        }
                        this.ID.Value = instanceMajor[0].ID.ToString();
                        this.CustomerID.Value = instanceMajor[0].CustomerID.ToString();
                        this.CustomerName.Value = instanceMajor[0].CustomerName;
                        this.DeliverDate.Value = instanceMajor[0].APDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].APDate.ToString("yyyy-MM-dd");
                        this.IsPerformance.SelectedValue = instanceMajor[0].ValueType;
                        this.ddlTrans.SelectedValue = instanceMajor[0].DevCustomerType;
                        this.ddlDevTpye.SelectedValue = instanceMajor[0].DevType;
                        if (IsPerformance.SelectedValue == "2")
                        {
                            hxcdTime.Value = instanceMajor[0].EXP4;
                        }
                        else if (IsPerformance.SelectedValue == "3")
                        {
                            cxcdTime.Value = instanceMajor[0].EXP4;
                        }
                        this.TopShare.Value = instanceMajor[0].TaskShare;
                        this.Explain.Value = instanceMajor[0].TaskExplain;
                        this.TransCustomerName.Value = instanceMajor[0].DevCustomerName;
                        this.txtTrainDateAddress.Value = instanceMajor[0].TrainDateAddress;
                        this.IsTrainAgain.SelectedValue = instanceMajor[0].IsTrainAgain;
                        this.txtCEODateAddress.Value = instanceMajor[0].CEODateAddress;
                        this.IsClassCEO.SelectedValue = instanceMajor[0].IsClassCEO;
                        this.IsWestPoint.SelectedValue = instanceMajor[0].IsWestPoint;
                        this.IsClassSign.SelectedValue = instanceMajor[0].IsClassSign;
                        this.IsPayOrder.SelectedValue = instanceMajor[0].IsPayOrder;
                        this.IsSalon.SelectedValue = instanceMajor[0].IsSalon;
                        this.UnSignClassReason.Value = instanceMajor[0].UnSignClassReason;
                        this.EPIsClassSign.SelectedValue = instanceMajor[0].EPIsClassSign;
                        this.txtEPDateAddress.Value = instanceMajor[0].EPDateAddress;
                        this.EPClassPay.SelectedValue = instanceMajor[0].EPClassPay;
                        this.EPPayDate.Value = instanceMajor[0].EPPayDate;
                        this.EPMoney.Value = instanceMajor[0].EPMoney.ToString();
                        this.txtMemember.Value = instanceMajor[0].ProductList;
                        this.PayMoneys.Value = instanceMajor[0].PayMoneyTotal.ToString();
                        this.cType.Value = instanceMajor[0].cType;
                        this.CValue.Value = instanceMajor[0].CValue;
                        this.Amount.Value = instanceMajor[0].Amount.ToString();
                        this.SaleRoad.Value = instanceMajor[0].SaleRoad;
                        this.txtLongDateAddress.Value = instanceMajor[0].LongDateAddress;
                        this.FactPayDate.Value = instanceMajor[0].FactTempDate;
                        this.txtLastAddress.Value = instanceMajor[0].LastAddress;
                        this.txtCEOAddress.Value = instanceMajor[0].CEOAddress;
                        this.ddlLastCEO.SelectedValue = instanceMajor[0].LastCEO;
                        ddlCEOClass.SelectedValue = instanceMajor[0].EXP1;
                        this.FirstNum.Value = instanceMajor[0].FirstNum;
                        this.PayDate.Value = instanceMajor[0].FirstDate;
                        this.txtPay.Value = instanceMajor[0].OrderMoney;
                        this.txtWestAddress.Value = instanceMajor[0].EXP3;//西点地址
                        this.ddlAgainPay.SelectedValue = instanceMajor[0].EXP2;//是否重复购买
                        this.txtAgainBuy.Value = instanceMajor[0].AgainBuyDate;
                        this.ddlAgainProductName.SelectedValue = instanceMajor[0].AgainBuyProduct;
                        this.CName.Value = instanceMajor[0].ICName.ToString();
                        this.txtCName.Value = instanceMajor[0].CName;
                        this.ZName.Value = instanceMajor[0].ICZName.ToString();
                        if (instanceMajor[0].EmployeeStatus == 3)
                        {
                            this.txtCName.Value = instanceMajor[0].CName+"<已离职>";
                            txtCName.Style.Add(HtmlTextWriterStyle.BackgroundColor, "yellow");
                        }
                        ddlYear.SelectedValue = instanceMajor[0].DevYear;
                        PackName.Value = instanceMajor[0].PackName;
                        this.txtZName.Value = instanceMajor[0].CZName;
                        this.CTel.Value = instanceMajor[0].CTel;
                        this.ZTel.Value = instanceMajor[0].CZTel;
                        this.ddlIsFirstSale.SelectedValue = instanceMajor[0].IsFirstSale;
                        this.OneMoney.Value = instanceMajor[0].OneMoney;
                        ddlProject.SelectedValue = instanceMajor[0].IsCOProject;
                        if (ddlProject.SelectedValue == "1") { 
                            ddlProjectType.Enabled = true; 
                            ProjectMoney.Disabled = false;
                            ProjectMoney.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
                        }
                        if (instanceMajor[0].zEStatus == 3)
                        {
                            this.txtZName.Value = instanceMajor[0].CZName + "<已离职>";
                            txtZName.Style.Add(HtmlTextWriterStyle.BackgroundColor, "yellow");
                        }
                        ddlProjectType.SelectedValue = instanceMajor[0].ProjectType.ToString();
                        ProjectMoney.Value = instanceMajor[0].ProjectMoney.ToString();
                        //string[] caption = instanceMajor[0].ProductList.Split(',');
                        //IList<V_CrmValuePay> ivcvp = bllDeliverMajorProject.GetAllCrmValuePay(instanceMajor[0].CustomerID, caption[0]);
                        //if (ivcvp != null && ivcvp.Count > 0)
                        //{
                        //    this.CName.Value = ivcvp[0].Name;
                        //    this.CTel.Value = ivcvp[0].MMobile;
                        //    this.ZTel.Value = ivcvp[0].Mobile;
                        //    this.ZName.Value = ivcvp[0].UName;
                        //}
                        IList<BaseEntity> _listDeliverTrainInfo = new List<BaseEntity>();
                        foreach (var _e in instanceMajor)
                        {
                            _listDeliverTrainInfo.Add(_e);
                        }
                        strDeliverTrainInfo = _listDeliverTrainInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    }
                    this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                    WorkFlowInsId.Value = this.GetStringFromQueryString("WorkflowInsId", "");
                }
                if (this.GetStringFromQueryString("view", "") == "check")
                {
                    btnAccept.Visible = true;
                }
            }
        }
        /// <summary>
        /// 绑定项目类型列表
        /// </summary>
        private void bindProjectType()
        {
            BTCrmCrustomerOrder order = new BTCrmCrustomerOrder();
            IList<V_CrmProduct> ListProduct = order.GetAllCrmCustomerProduct("3AEDE3F6-1915-48D5-AFB2-C9380DB26813");
            ddlProjectType.DataSource = ListProduct;
            ddlProjectType.DataTextField = "Caption";
            ddlProjectType.DataValueField = "Productid";
            ddlProjectType.DataBind();
        }
        /// <summary>
        /// 初始化流程列表
        /// </summary>
        /// <param name="workflowInsId"></param>
        public void Inits(string workflowInsId)
        {
            DataTable dt = client.GetWorkflowdetail(workflowInsId);
            //if (dt == null || dt.Rows.Count == 0)
            //{
            //    gvworkflowMessage.Visible = false;
            //}
            //else
            //{
            gvworkflowMessage.DataSource = dt;
            gvworkflowMessage.DataBind();
            //}
        }
        /// <summary>
        /// 知道了
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            //初始化服务实例
            WorkFlowServiceClient workFlowServiceClient = new WorkFlowServiceClient();
            strWorkTaskInsId = this.GetStringFromQueryString("WorktaskInsId", "");
            string msg1 = workFlowServiceClient.Run(this.User.LoginName, strOperatorInstanceId, "提交");
            Response.Redirect("../BasePages/tooltip.aspx?worktaskInstanceId=" + strWorkTaskInsId);
        }
        /// <summary>
        /// 任务实例ID
        /// </summary>
        protected string strWorkTaskInsId { get; set; }
        public string StrId
        { get { return strId; } }
        public string Flag
        { get { return this.GetStringFromQueryString("flag", ""); } }
        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strWorkFlowId
        { get { return this.GetStringFromQueryString("wfid", "") == "" ? wfid : this.GetStringFromQueryString("wfid", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        { get { return this.GetStringFromQueryString("wtid", "") == "" ? wtid : this.GetStringFromQueryString("wtid", ""); } }
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        public string strOperStatus
        {
            get { return this.GetStringFromQueryString("OperStatus", ""); }
        }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }
    }
}